'use strict';

/* Controllers */
// signin controller
app.controller('BaseInfoController', ['$rootScope', '$scope', '$state', '$cookieStore', 'Upload', 'BaseInfoService', 'dbUtils', function ($rootScope, $scope, $state, $cookieStore, upload, baseInfoService, dbUtils) {
    //init
    $scope.addr = {};
    $scope.info = {};
    $scope.innerPics=[];

    var init =true;

    baseInfoService.getDetail().then(function(response){
        console.log(response);
        if(response.code == 0){
            $scope.info = response.data;
            $scope.typeSelect = $scope.info.type;
            $scope.innerPics = response.data.innerPic.split(",");
            var idcardpics = $scope.info.idcardPic.split(",");
            $scope.info.idcardPic1 = idcardpics[0];
            $scope.info.idcardPic2 = idcardpics[1];

            baseInfoService.getProvinces().then(function (response) {
                if (response.code == 0) {
                    $scope.provinces = response.data;
                    if($scope.info.provinceId && init){
                        $scope.addr.provinceSelect = $scope.info.provinceId;
                        //必须一起设置才会联动，并且选中
                        $scope.addr.province = $scope.info.provinceId;
                    }
                }
            });
        }
    },function(fail){

    });

    baseInfoService.getStoreTypes().then(function (response) {
        if (response.code == 0) {
            $scope.shopCategory = response.data;
            if($scope.info.categoryId){
                $scope.typeSelect = $scope.info.type;
            }
        }
    });

    $scope.$watch("addr.province", function () {
        $scope.addr.city = "";
        if ($scope.addr && $scope.addr.province) {
            baseInfoService.getCities($scope.addr.province).then(function (response) {
                $scope.cities = response.data;
                $scope.areas = [];
                if($scope.info.cityId && init){
                    $scope.addr.city = $scope.info.cityId;
                    $scope.addr.citySelect = $scope.info.cityId;
                }
            });
        }
    });

    $scope.$watch("addr.city", function () {
        if ($scope.addr && $scope.addr.city) {
            baseInfoService.getAreas($scope.addr.city).then(function (response) {
                $scope.areas = response.data;

                if($scope.info.areaId && init){
                    init =false;
                    $scope.addr.areaSelect = $scope.info.areaId;
                }
            });
        }
    });



    $scope.upload = function (file, type, model) {
        if (file) {
            upload.upload({
                url: '/upload/img',
                fields: {'type': type},
                file: file
            }).progress(function (evt) {

            }).success(function (data, status, headers, config) {
                $scope['info'][model] = data.data;
            }).error(function (data, status, headers, config) {
                dbUtils.error("上传失败", "提示");
            })
        }
    };
    $scope.removeImg = function (model) {
        $scope['info'][model] = null;
    }

    $scope.uploadMulti = function (files,type) {
        if (files && files.length) {
            for (var i = 0; i < files.length; i++) {
                if($scope.innerPics.length > 10){
                    return;
                }

                upload.upload({
                    url: '/upload/img',
                    fields: {'type': type},
                    file: files[i]
                }).progress(function (evt) {

                }).success(function (data, status, headers, config) {
                    $scope['innerPics'].push(data.data);
                }).error(function (data, status, headers, config) {
                    dbUtils.error("上传失败", "提示");
                })
            }
        }
    }

    $scope.removeMultiImg = function(model,i){
        $scope[model].splice(i,1);
    }


    $scope.showImg = function (event) {
        dbUtils.showImg(event.target.currentSrc);
    }


    $scope.submitApply = function(){
        $scope.info.innerPic = $scope.innerPics.join(",");
        var idcardPicArr = [];
        if($scope.info.idcardPic1){
            idcardPicArr.push($scope.info.idcardPic1);
        }
        if($scope.info.idcardPic2){
            idcardPicArr.push($scope.info.idcardPic2);
        }
        $scope.info.idcardPic = idcardPicArr.join(",");

        baseInfoService.setDetail($scope.info).then(function(response){
            if(response.code == 0){
                dbUtils.success("提交成功！","提示");
            }else{
                dbUtils.success(response.message,"提示");
            }
        });
    }
}])
;